<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>以千位分隔符方式格式化一个数字</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.number-format" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">number_format</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">number_format</span> &mdash; <span class="dc-title">以千位分隔符方式格式化一个数字</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.number-format-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>number_format</strong></span>
    ( <span class="methodparam"><span class="type">float</span> <code class="parameter">$number</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$decimals</code><span class="initializer"> = 0</span></span>
  ] ) : <span class="type">string</span></div>

  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>number_format</strong></span>
    ( <span class="methodparam"><span class="type">float</span> <code class="parameter">$number</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$decimals</code><span class="initializer"> = 0</span></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$dec_point</code><span class="initializer"> = &quot;.&quot;</span></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$thousands_sep</code><span class="initializer"> = &quot;,&quot;</span></span>
   ) : <span class="type">string</span></div>

  <p class="para rdfs-comment">
   本函数可以接受1个、2个或者4个参数（注意：不能是3个）:
  </p>
  <p class="para">
   如果只提供第一个参数，<code class="parameter">number</code>的小数部分会被去掉
   并且每个千位分隔符都是英文小写逗号&quot;,&quot;
  </p>
  <p class="para">
   如果提供两个参数，<code class="parameter">number</code>将保留小数点后的位数到你设定的值，其余同楼上
  </p>
  <p class="para">
   如果提供了四个参数，<code class="parameter">number</code>
   将保留<code class="parameter">decimals</code>个长度的小数部分,
   小数点被替换为<code class="parameter">dec_point</code>，千位分隔符替换为<code class="parameter">thousands_sep</code>
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.number-format-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">number</code></dt>

     <dd>

      <p class="para">
       你要格式化的数字
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">decimals</code></dt>

     <dd>

      <p class="para">
       要保留的小数位数
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">dec_point</code></dt>

     <dd>

      <p class="para">
       指定小数点显示的字符
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">thousands_sep</code></dt>

     <dd>

      <p class="para">
       指定千位分隔符显示的字符
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.number-format-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   格式化以后的 <code class="parameter">number</code>.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.number-format-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.4.0</td>
       <td>
        This function now supports multiple bytes in
        <code class="parameter">dec_point</code> and
        <code class="parameter">thousands_sep</code>. Only the first byte of each
        separator was used in older versions.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.number-format-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5928">
    <p><strong>Example #1 <span class="function"><strong>number_format()</strong></span> Example</strong></p>
    <div class="example-contents"><p>
     For instance, French notation usually use two decimals,
     comma (&#039;,&#039;) as decimal separator, and space (&#039; &#039;) as
     thousand separator.  The following example demonstrates various way to format a number:
    </p></div>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$number&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1234.56</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;english&nbsp;notation&nbsp;(default)<br /></span><span style="color: #0000BB">$english_format_number&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">number_format</span><span style="color: #007700">(</span><span style="color: #0000BB">$number</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//&nbsp;1,235<br /><br />//&nbsp;French&nbsp;notation<br /></span><span style="color: #0000BB">$nombre_format_francais&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">number_format</span><span style="color: #007700">(</span><span style="color: #0000BB">$number</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">','</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'&nbsp;'</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//&nbsp;1&nbsp;234,56<br /><br /></span><span style="color: #0000BB">$number&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1234.5678</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;english&nbsp;notation&nbsp;without&nbsp;thousands&nbsp;separator<br /></span><span style="color: #0000BB">$english_format_number&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">number_format</span><span style="color: #007700">(</span><span style="color: #0000BB">$number</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'.'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//&nbsp;1234.57<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>

 
 <div class="refsect1 changelog" id="refsect1-function.number-format-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>7.2.0</td>
       <td>
        <span class="function"><strong>number_format()</strong></span> 现在无法返回 <em>-0</em>，之前可能返回 <em>-0</em>，因为 <code class="parameter">number</code> 可能会是 <em>-0.01</em>。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.number-format-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="money_format.html" class="function" rel="rdfs-seeAlso">money_format()</a> - 将数字格式化成货币字符串</span></li>
    <li class="member"><span class="function"><a href="sprintf.html" class="function" rel="rdfs-seeAlso">sprintf()</a> - Return a formatted string</span></li>
    <li class="member"><span class="function"><a href="printf.html" class="function" rel="rdfs-seeAlso">printf()</a> - 输出格式化字符串</span></li>
    <li class="member"><span class="function"><a href="sscanf.html" class="function" rel="rdfs-seeAlso">sscanf()</a> - 根据指定格式解析输入的字符</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>